//
//  SEGServiceEvaluationViewModel.m
//  AFNetworking
//
//  Created by Clavis on 2020/11/25.
//

#import "SEGServiceEvaluationViewModel.h"
#import "SEGServiceEvaluationManager.h"
#import "SEGServiceEvaluationModel.h"

@interface SEGServiceEvaluationViewModel ()

@property (nonatomic, assign) __block NSInteger pageNo;
@property (nonatomic, assign) __block NSInteger totalPageNo;
@property (nonatomic, assign) NSInteger praiseNum;
@property (nonatomic, assign) NSInteger criticizeNum;

@end

@implementation SEGServiceEvaluationViewModel

- (instancetype)init {
    self = [super init];
    if (self) {
        self.totalPageNo = -1;
    }
    return self;
}

/// 请求服务评论列表
- (RACCommand *)requestEvaluationListCommand {
    if (!_requestEvaluationListCommand) {
        @weakify(self);
        _requestEvaluationListCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
            
            bool isRefresh = [input boolValue];
            if (isRefresh) {
                self.pageNo = 1;
            } else {
                self.pageNo ++;
            }
            
            @strongify(self);
            return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
                // 已是最后一页就不再继续请求下页
                if (!isRefresh && self.totalPageNo != -1 && self.pageNo > self.totalPageNo) {
                    [subscriber sendCompleted];
                }
                [SEGServiceEvaluationManager requestEvaluationListWithPageNo:self.pageNo finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
                    if (!isSuccessed) {
                        NSError *error = [NSError errorWithDomain:NSURLErrorDomain code:[codeType integerValue] userInfo:@{NSLocalizedDescriptionKey:errorMessage}];
                        [subscriber sendError:error];
                        return;
                    }
                    
                    NSDictionary *ratingCollect = result[@"data"][@"ratingCollect"];
                    NSString *weekTime = ratingCollect[@"weekTime"];
                    NSDecimalNumber *starValue = [NSDecimalNumber decimalNumberWithString:ValueToString(ratingCollect[@"avgLevel"])];
                    NSString *praiseNumber = ValueToString(ratingCollect[@"praiseNum"]);
                    NSString *criticismNumber = ValueToString(ratingCollect[@"criticizeNum"]);
                    self.totalPageNo = [result[@"data"][@"paginator"][@"totalPages"] integerValue];
                    self.criticizeNum = [result[@"data"][@"ratingCollect"][@"criticizeNum"] integerValue];
                    self.praiseNum = [result[@"data"][@"ratingCollect"][@"praiseNum"] integerValue];
                                             
                    NSMutableArray<SEGServiceEvaluationModel *> *tempArray = [NSMutableArray array];
                    for (NSDictionary *subDic in result[@"data"][@"ratingInfoList"]) {
                        SEGServiceEvaluationModel *model =  [SEGServiceEvaluationModel yy_modelWithDictionary:subDic];
                        [tempArray addObject:model];
                    }
                    
                    if (isRefresh) {
                        self.dataSource = tempArray;
                    } else {
                        [self.dataSource appendObjects:tempArray];
                    }
                    
                    [subscriber sendNext:RACTuplePack(weekTime,starValue,praiseNumber,criticismNumber)];
                    [subscriber sendCompleted];
                }];
                return nil;
            }];
        }];
    }
    return _requestEvaluationListCommand;
}

/// 提交评价
- (RACCommand *)requestSumbitEvaluationCommand {
    if (!_requestSumbitEvaluationCommand) {
        @weakify(self);
        _requestSumbitEvaluationCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
            RACTupleUnpack(NSNumber *ratingType, NSNumber *ratingStarLevel) = input;
            @strongify(self);
            return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
                NSDictionary *param = @{@"ratingType" : ratingType,
                                        @"praiseNum" : @(self.praiseNum),
                                        @"criticizeNum" : @(self.criticizeNum),
                                        @"ratingStarLevel" : ratingStarLevel,
                                        @"communityId" : kAccountCommunityID,
                                        @"platform" : kAppPlatformCode
                                        };
                [SEGServiceEvaluationManager requestSubmitEvaluationWithParam:param finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
                    if (!isSuccessed) {
                        NSError *error = [NSError errorWithDomain:NSURLErrorDomain code:[codeType integerValue] userInfo:@{NSLocalizedDescriptionKey:errorMessage}];
                        [subscriber sendError:error];
                    }
                    [subscriber sendNext:errorMessage];
                    [subscriber sendCompleted];
                }];
                return nil;
            }];
        }];
    }
    return _requestSumbitEvaluationCommand;
}
@end
